const {ccclass, property} = cc._decorator;

/**语音聊天控制脚本 */
@ccclass
export default class Voice extends cc.Component {

    /**按下录音的开始时间 */
    startTouchTime = 0;


    /**录音UI的容器 */
    voice: cc.Node = null;

    /**录音失败的容器 */
    voiceFailed: cc.Node = null;

    /**更新音量大小显示的时间记录暂存器（300毫秒更新一次） */
    lastCheckTime = -1;

    /**录音中的UI的容器 */
    volume: cc.Node = null;

    /**录音时长的节点 */
    timeBar: cc.Node = null;

    /**最大录音时长 */
    MAX_TIME = 15000;

    /**原生系统内存储的缓存录音文件名 */
    recordFilename = 'record.amr';

    onLoad() {
        this.voice = cc.find('Canvas/voice');
        this.voice.active = false;

        this.voiceFailed = cc.find('Canvas/voice/voice_failed');
        this.voiceFailed.active = false;

        this.timeBar = cc.find('Canvas/voice/time');
        this.timeBar.scaleX = 0.0;

        this.volume = cc.find('Canvas/voice/volume');
        for(let i = 0; i < this.volume.childrenCount; i++){
            this.volume.children[i].active = false;
        }

        /**录音失败的确定按钮 */
        const btnVoiceFailed = cc.find('Canvas/voice/voice_failed/btn_ok');
        if(btnVoiceFailed){
            cc.vv.utils.addClickEvent(btnVoiceFailed, this.node, 'Voice', 'onBtnOkClicked');
        }

        /**录音按钮 */
        const btnVoice = cc.find('Canvas/btn_voice');
        if (btnVoice){
            btnVoice.on(cc.Node.EventType.TOUCH_START, () => {
                cc.vv.voiceMgr.prepare(this.recordFilename);
                this.startTouchTime = Date.now();
                this.voice.active = true;
                this.voiceFailed.active = false;
            });
            btnVoice.on(cc.Node.EventType.TOUCH_END, () => {
                if(Date.now() - this.startTouchTime < 1000){
                    this.voiceFailed.active = true;
                    cc.vv.voiceMgr.cancel();
                }
                else{
                    this.onVoiceOk();
                }
                this.startTouchTime = 0;
            });
            btnVoice.on(cc.Node.EventType.TOUCH_CANCEL, () => {
                cc.vv.voiceMgr.cancel();
                this.startTouchTime = 0;
                this.voice.active = false;
            });
        }
    }


    /**录音失败后的确定按钮的点击事件 */
    onBtnOkClicked(){
        this.voice.active = false;
    }


    /**录音完成 */
    onVoiceOk(){
        if(this.startTouchTime !== 0){
            cc.vv.voiceMgr.release();
            /**录音时长 */
            const time = Date.now() - this.startTouchTime;
            /**音频数据 */
            const msg = cc.vv.voiceMgr.getVoiceData(this.recordFilename);
            cc.vv.net.send(cc.vv.game_event_map.VOICE_MSG, {msg, time});
        }
        this.voice.active = false;
    }


    update(dt: number){
        // 更新音量大小显示
        if(this.voice.active && !this.voiceFailed.active){
            if(Date.now() - this.lastCheckTime > 300){
                this.volume.children.forEach((v) => {
                    v.active = false;
                });
                const v = cc.vv.voiceMgr.getVoiceLevel(7);
                if(v >= 1 && v <= 7){
                    this.volume.children[v - 1].active = true;
                }
                this.lastCheckTime = Date.now();
            }
        }

        // 更新录音时长的显示
        if(this.startTouchTime != 0){
            const time = Date.now() - this.startTouchTime;
            if(time >= this.MAX_TIME){
                this.onVoiceOk();
                this.startTouchTime = 0;
            }
            else{
                const precent = time / this.MAX_TIME;
                this.timeBar.scaleX = 1 - precent;
            }
        }
    }
}
